﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Linq;
using Throw_In.SampleData;
using Throw_In.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Throw_In
{
    public partial class PageOne : PhoneApplicationPage
    {
        XDocument XDoc;
        public PageOne()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string strItemIndex = "0";

            if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))

                pivot.SelectedItem = pivot.Items[Convert.ToInt32(strItemIndex)];

            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.AllDataModel.IsDataLoaded)
            {
                App.AllDataModel.LoadData();
            }
            ObservableCollection<SimpleCategory> Cat = new ObservableCollection<SimpleCategory>();
            SimpleCategory _cat = new SimpleCategory();
            _cat.CategoryName = "Internasional";
            _cat.Id = 0;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "Timnas";
            _cat.Id = 1;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "Bolatainment";
            _cat.Id = 2;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "Funatik";
            _cat.Id = 3;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "News";
            _cat.Id = 4;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "Highlight";
            _cat.Id = 5;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "Fans";
            _cat.Id = 6;
            Cat.Add(_cat);
            _cat = new SimpleCategory();
            _cat.CategoryName = "BolalobTV";
            _cat.Id = 7;
            Cat.Add(_cat);
            MainListBox3.ItemsSource = Cat;

            ObservableCollection<SimpleCategory> CatCom = new ObservableCollection<SimpleCategory>();
            SimpleCategory _cats = new SimpleCategory();
            _cats.CategoryName = "Viking";
            _cats.Id = 0;
            CatCom.Add(_cats);
            _cats = new SimpleCategory();
            _cats.CategoryName = "Bonek";
            _cats.Id = 1;
            CatCom.Add(_cats);
            _cats = new SimpleCategory();
            _cats.CategoryName = "Bomber";
            _cats.Id = 2;
            CatCom.Add(_cats);
            _cats = new SimpleCategory();
            _cats.CategoryName = "The Jak";
            _cats.Id = 3;
            CatCom.Add(_cats);
            _cats = new SimpleCategory();
            _cats.CategoryName = "Aremania";
            _cats.Id = 4;
            CatCom.Add(_cats);
            KomunitasListBox.ItemsSource = CatCom;

            ObservableCollection<ItemViewModel> Items = new ObservableCollection<ItemViewModel>();
            XDoc = XDocument.Load("data.xml");
            List<ItemViewModel> list = new List<ItemViewModel>();
            foreach (var member in XDoc.Descendants("item"))
            {
                ItemViewModel item = new ItemViewModel();
                item.Url = member.Element("url_alias").Value;
                item.Id = member.Element("node_id").Value;
                item.Content = member.Element("body").Value;
                item.Title = member.Element("title").Value;
                item.Description = member.Element("description").Value;
                foreach (var members in member.Descendants("image"))
                {
                    item.Image = members.Element("medium").Value;
                }

                Items.Add(item);
            }
            MainListBox.ItemsSource = getTopNews();
            MainListBox2.ItemsSource = getRandom();
            PlayerListBox.ItemsSource = App.AllDataModel.Player;
            TeamListBox.ItemsSource = App.AllDataModel.Team;

        }
        private ObservableCollection<ItemViewModel> getTopNews()
        {
            ObservableCollection<ItemViewModel> retVal = new ObservableCollection<ItemViewModel>();
            ObservableCollection<ItemViewModel> temp = getSortedNews(App.AllDataModel.AllItems);
            retVal.Add(temp[0]);
            retVal.Add(temp[1]);
            retVal.Add(temp[2]);
            retVal.Add(temp[3]);
            retVal.Add(temp[4]);
            retVal.Add(temp[5]);
            return retVal;
        }
        private ObservableCollection<ItemViewModel> getSortedNews(ObservableCollection<ItemViewModel> items)
        {
            ObservableCollection<ItemViewModel> retVal = new ObservableCollection<ItemViewModel>();
            int count;
            bool found = false;
            foreach (var item in items)
            {
                count = 0;
                if (retVal.Count == 0)
                {
                    retVal.Add(item);
                }
                else
                {
                    found = false;
                    foreach (var itemVal in retVal)
                    {
                        if (item.View >= itemVal.View)
                        {
                            retVal.Insert(count, item);
                            found = true;
                            break;
                        }
                        else count++;
                    }
                    if(!found) retVal.Insert(count, item);
                    
                }
            }
            return retVal;
        }
        private ObservableCollection<ItemViewModel> getRandom()
        {
            ObservableCollection<ItemViewModel> retVal = new ObservableCollection<ItemViewModel>();
            Random rand = new Random();
            retVal.Add(getItem(App.AllDataModel.Internasional, rand.Next(App.AllDataModel.Internasional.Count)));
            retVal.Add(getItem(App.AllDataModel.Timnas, rand.Next(App.AllDataModel.Timnas.Count)));
            retVal.Add(getItem(App.AllDataModel.Bolatainment, rand.Next(App.AllDataModel.Bolatainment.Count)));
            retVal.Add(getItem(App.AllDataModel.Funatik, rand.Next(App.AllDataModel.Funatik.Count)));
            retVal.Add(getItem(App.AllDataModel.News, rand.Next(App.AllDataModel.News.Count)));
            retVal.Add(getItem(App.AllDataModel.Highlight, rand.Next(App.AllDataModel.Highlight.Count)));
            return retVal;
        }
        private ItemViewModel getItem(ObservableCollection<ItemViewModel>items, int index)
        {
            int count = 0;
            foreach (var item in items)
            {
                if (index == count)
                {
                    return item;
                }else count++;
            }
            return null;
        }


        private void TopChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)MainListBox.SelectedItem).Id;
            // Navigate to the new page
            //            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            PhoneApplicationService.Current.State["Item"] = MainListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));

            // Reset selected index to -1 (no selection)
            
        }
        private void aboutClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }
        private void shareClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FacebookPage.xaml?global=yes", UriKind.Relative));
        }
        private void tileClickHandler(object sender, EventArgs e)
        {
            //pin();
        }
        private void searchClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SearchResult.xaml", UriKind.Relative));
        }
        private void bookmarksClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=1", UriKind.Relative));
        }
        private void recentClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=2", UriKind.Relative));
        }
        private void infoClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=3", UriKind.Relative));

        }

        private void appbar_StateChanged_1(object sender, ApplicationBarStateChangedEventArgs e)
        {

        }
        private void RandomChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)MainListBox2.SelectedItem).Id;
            PhoneApplicationService.Current.State["Item"] = MainListBox2.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void CatChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((SimpleCategory)MainListBox3.SelectedItem).Id;
            System.Diagnostics.Debug.WriteLine(guid);
            if(guid==0)
                NavigationService.Navigate(new Uri("/GroupTwo.xaml", UriKind.Relative));
            else
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=" + guid, UriKind.Relative));
        }

    }
}